set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
/* *******************************************************************
** DATE :
** Writer :
** Memo :
******************************************************************** */
CREATE PROCEDURE [dbo].[TSP_MyEvent_GetList]
(
	@IsTransaction	tinyint			= 0
,	@ErrorCode		int				= 0		OUTPUT
,	@ErrorMessage	nvarchar( 1000 )= ''	OUTPUT

,	@n4UserNo	int	= 1	
,	@PageNo	int	= 1	
,	@PageSize	tinyint	= 10	
,	@CntRow	int	= NULL	OUTPUT
,	@CntTotal	int	= NULL	OUTPUT

)
as
Set NOCOUNT ON

-- Frk Default Value
DECLARE @FrkDateNow DateTime
DECLARE @FrkRowCount int
DECLARE @FrkTranCount int
DECLARE @FrkErrorCode int
DECLARE @FrkErrorMessage nvarchar( 1000 )
Set @FrkDateNow = GetDate()
set @FrkTranCount = @@TRANCOUNT
-- Frk Check


If ( @IsTransaction = 0 )
Begin
	If ( @FrkTranCount < 1 )
	Begin
		Set @FrkErrorCode = -1
		Set @FrkErrorMessage = 'Transaction.'
		goto ERROR
	End
	Else If ( @FrkTranCount > 1 )
	Begin
		Set @FrkErrorCode = -2
		Set @FrkErrorMessage = 'Transaction.'
		goto ERROR
	End
End
Else
Begin
	If ( @FrkTranCount <> 0 )
	Begin
		Set @FrkErrorCode = -3
		Set @FrkErrorMessage = 'Transaction.'
		goto ERROR
	End
	Begin Tran
End

-- Implementation

Select
	@Cnttotal = count(*)
FROM
	TB_EventCommentWriter AS W WITH ( NOLOCK )
JOIN	TB_Event AS E WITH ( NOLOCK )
ON	E.n4EventSN = W.n4EventSN
WHERE
	W.n4UserNo = @n4UserNo

DECLARE @SkipPage INT
SET @SkipPage = ( ( @PageNo - 1 ) * @PageSize )


IF ( @PageNo = 1 )
BEGIN
	SELECT	Top( @PageSize )
		E.n4EventSN, 
		E.dtEventStart, 
		E.dtEventEnd, 
		E.dtPublicationDate, 
		E.strImageUrl, 
		E.strBigImageUrl, 
		E.strTitle, 
		E.strContent, 
		E.isShow, 
		E.n4TotalCommentCount, 
		E.n4ParticipationCount, 
		E.n4UniqueUserCount, 
		E.dtCreateDate, 
		E.strManagerName,
		W.dtModifyDate,
		W.n4UserNo,
		W.n4EventUserSN
	FROM
		TB_EventCommentWriter AS W WITH ( NOLOCK )
	JOIN	TB_Event AS E WITH ( NOLOCK )
	ON	E.n4EventSN = W.n4EventSN
	WHERE
		W.n4UserNo = @n4UserNo
	ORDER BY
		W.n4UserNo DESC, W.n4EventUserSN DESC
END
ELSE
BEGIN
	SELECT	Top( @PageSize )
	E.n4EventSN, 
	E.dtEventStart, 
	E.dtEventEnd, 
	E.dtPublicationDate, 
	E.strImageUrl, 
	E.strBigImageUrl, 
	E.strTitle, 
	E.strContent, 
	E.isShow, 
	E.n4TotalCommentCount, 
	E.n4ParticipationCount, 
	E.n4UniqueUserCount, 
	E.dtCreateDate, 
	E.strManagerName,
	E.dtModifyDate,
	E.n4UserNo,
	E.n4EventUserSN
	FROM
	(
		SELECT	Top( @PageSize )
			E.n4EventSN, 
			E.dtEventStart, 
			E.dtEventEnd, 
			E.dtPublicationDate, 
			E.strImageUrl, 
			E.strBigImageUrl, 
			E.strTitle, 
			E.strContent, 
			E.isShow, 
			E.n4TotalCommentCount, 
			E.n4ParticipationCount, 
			E.n4UniqueUserCount, 
			E.dtCreateDate, 
			E.strManagerName,
			W.dtModifyDate,
			W.n4UserNo,
			W.n4EventUserSN
		,	ROW_NUMBER() OVER ( ORDER BY
			W.n4UserNo DESC, W.n4EventUserSN DESC
		) AS ROWNUM
		FROM
			TB_EventCommentWriter AS W WITH ( NOLOCK )
		JOIN	TB_Event AS E WITH ( NOLOCK )
		ON	E.n4EventSN = W.n4EventSN
		WHERE
			W.n4UserNo = @n4UserNo
		ORDER BY
			W.n4UserNo DESC, W.n4EventUserSN DESC
	)	E
	WHERE
		ROWNUM > @SkipPage
	AND	ROWNUM <= @SkipPage + @PageSize
	ORDER	BY
		n4UserNo DESC, n4EventUserSN DESC
	
END









-- Frk Error Check
SELECT
	@FrkErrorCode = @@ERROR
,	@FrkRowCount = @@RowCount

-- End

COMPLETE:
	If ( @IsTransaction <> 0 )
	Begin
		If ( @@TRANCOUNT = 1 )
		Begin
			Commit Tran
		End
	End
	SET @ErrorCode = 0
	SET @ErrorMessage = ''
	return 0
ERROR:
	If ( @IsTransaction <> 0 )
	Begin
		If ( @@TRANCOUNT = 1 )
		Begin
			Rollback Tran
		End
	End
	SET @ErrorCode = @FrkErrorCode
	SET @ErrorMessage = @FrkErrorMessage
	return @FrkErrorCode
